/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.runtime.component
{
	 import flash.display.Sprite;
	 import flash.external.ExternalInterface;
	 import flash.net.URLRequest;
	 import flash.net.navigateToURL;

/****
 * 匹配对浏览器的下载组件.
 * 解决IE7无法用navigateToURL
 * 弹出新窗口下载文件的错误
 */
 public class OpenWindow {
  
    import flash.display.Sprite;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.external.ExternalInterface;
	
	
		public function OpenWindow(url:String, window:String="_blank", features:String=""){
			  
			var WINDOW_OPEN_FUNCTION:String = "window.open";
			var myURL:URLRequest = new URLRequest(url);			
			var browserName:String = getBrowserName();
			
			if(getBrowserName() == "Firefox"){
				//EDIApplication.outputDebug("这次是Firefox");
				ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
			}
			//If IE, 
			else if(browserName == "IE"){
				//EDIApplication.outputDebug("这次是IE");
				ExternalInterface.call(WINDOW_OPEN_FUNCTION, url, window, features);
				//ExternalInterface.call("function setWMWindow() {window.open(&apos;" + url + "&apos;);}");
			}
			//If Safari  
			else if(browserName == "Safari"){
				//EDIApplication.outputDebug("这次是Safari");			  
				navigateToURL(myURL, window);
			}
			//If Opera 
			else if(browserName == "Opera"){
				//EDIApplication.outputDebug("这次是Opera");
				navigateToURL(myURL, window); 
			} else {
				//EDIApplication.outputDebug("这次啥也不是");
				navigateToURL(myURL, window);
			}
			
		}
		/****
		 * 获得浏览器的类型
		 *
		 */
		private function getBrowserName():String{
			var browser:String;
			
			var browserAgent:String = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
		
			 
			if(browserAgent != null && browserAgent.indexOf("Firefox") >= 0) {
				browser = "Firefox";
			} 
			else if(browserAgent != null && browserAgent.indexOf("Safari") >= 0){
				browser = "Safari";
			}			 
			else if(browserAgent != null && browserAgent.indexOf("MSIE") >= 0){
				browser = "IE";
			}		 
			else if(browserAgent != null && browserAgent.indexOf("Opera") >= 0){
				browser = "Opera";
			}
			else {
				browser = "Undefined";
			}
			return browser;
		}
	}
}